package com.loong.musicdemo.util.dataDeal;

import android.util.Log;

import com.loong.musicdemo.model.Album;
import com.loong.musicdemo.model.MV;
import com.loong.musicdemo.model.Singer;
import com.loong.musicdemo.util.httpUtil.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SingerDeal {
    /**
     * 获取歌手的50首单曲
     * @param singerId 歌手id
     * @return 歌曲id集合
     */
    public static List<String> getSingerSongIds(String singerId){
        String url = "http://47.112.224.146:3000/artists?id="+singerId;
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        List<String> ids = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(response);
            JSONArray hotSongs = object.getJSONArray("hotSongs");
            for (int i = 0;i<hotSongs.length();i++){
                JSONObject hotSong = hotSongs.getJSONObject(i);
                ids.add(hotSong.getString("id"));
            }
            return ids;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return ids;
    }

    /**
     * 每次获取歌手20个专辑
     * @param singerId 歌手id
     * @return 歌手专辑集合
     */
    public static List<Album> getSingerAlbumIds(String singerId){
        String url = "http://47.112.224.146:3000/artist/album?id="+singerId+"&limit=20";
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        List<Album> albumList = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(response);
            JSONArray hotAlbums = object.getJSONArray("hotAlbums");
            for (int i = 0;i<hotAlbums.length();i++){
                JSONObject hotAlbum = hotAlbums.getJSONObject(i);
                Album album = new Album();
                album.setAlbumId(hotAlbum.getString("id"));
                album.setAlbumPicUrl(hotAlbum.getString("picUrl"));
                album.setAlbumName(hotAlbum.getString("name"));
                album.setSongIdsInAlbum(AlbumDeal.getSongIdOfAlbum(album.getAlbumId()));
                album.setAlbumCommentCount(AlbumDeal.getAlbumCommentCount(album.getAlbumId()));
                albumList.add(album);
            }
            return albumList;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return albumList;
    }

    /**
     * 获取歌手的信息
     * @param id 歌手账户id
     * @return 歌手信息
     */
    public static Singer getSingerById(String id){
        String url = "http://47.112.224.146:3000/user/detail?uid="+id;
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        Singer singer = new Singer();
        try {
            JSONObject object = new JSONObject(response);
            JSONObject profile = object.getJSONObject("profile");
            singer.setId(profile.getString("artistId"));
            singer.setName(profile.getString("nickname"));
            singer.setAccountId(id);
            singer.setPicUrl(profile.getString("avatarUrl"));
            singer.setImg1v1Url(profile.getString("avatarUrl"));
            singer.setFolloweds(profile.getString("followeds"));
            singer.setFollows(profile.getString("follows"));
            return singer;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return singer;
    }

    /**
     * 获取歌手的mv
     * @param singerId 歌手id
     * @return 歌手mv信息
     */
    public static List<MV> getSingerMvById(String singerId){
        String url = "http://47.112.224.146:3000/artist/mv?id="+singerId;
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        List<MV> mvList = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(response);
            JSONArray mvs = object.getJSONArray("mvs");
            for(int i = 0;i<mvs.length();i++){
                MV mv = new MV();
                JSONObject m = mvs.getJSONObject(i);
                mv.setCoverImg(m.getString("imgurl"));
                mv.setMvID(m.getString("id"));
                mv.setMvName(m.getString("name"));
                mv.setPlayCount(m.getString("playCount"));
                mv.setMvUrl(MvDeal.getMvUrlByMvId(mv.getMvID()));
                mv.setDuration(m.getString("duration"));
                mv.setArtistName(m.getString("artistName"));
                mvList.add(mv);
            }
            return mvList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mvList;
    }

    /**
     * 获取歌手的简介信息
     * @param singerId 歌手id
     * @return 一个键值对
     */
    public static Map<String,String> getSingerInfoById(String singerId){
        String url = "http://47.112.224.146:3000/artist/desc?id="+singerId;
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        Map<String,String> info = new HashMap<String, String>();
        try {
            JSONObject object = new JSONObject(response);
            JSONArray introduction = object.getJSONArray("introduction");
            info.put("歌手简介",object.getString("briefDesc"));
            for(int i = 0;i<introduction.length();i++){
                JSONObject item = introduction.getJSONObject(i);
                info.put(item.getString("ti"),item.getString("txt"));
            }
            return info;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }
}
